package nought.com.relaceelementinhtmldemo;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.webkit.WebView;


/**
 * WebView父组件
 * 用于传递上下滑动的参数
 */
public class ScrollWebView extends WebView {

    public ScrollWebView(Context context) {
        super(context);
    }

    public ScrollWebView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public ScrollWebView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        // webview的高度
        float webcontent = getContentHeight() * getScale();
        // 当前webview的高度
        float webnow = getHeight() + getScrollY();
//        int distanceScrolled = oldl - oldt;
        if (Math.abs(webcontent - webnow) < 1) {
            //处于底端
            Log.e("测试","处于底端");
            if(mOnScrollChangeListener !=null){
                mOnScrollChangeListener.onPageEnd(l, t, oldl, oldt);
            }
        } else if (getScrollY() == 0) {
            //处于顶端
            Log.e("测试","处于顶端");
            if(mOnScrollChangeListener !=null){
                mOnScrollChangeListener.onPageTop(l, t, oldl, oldt);
            }
        } else {
            if(mOnScrollChangeListener !=null){
                mOnScrollChangeListener.onScrollChanged(l, t, oldl, oldt);
            }
        }
//        float slideDeviation = (mWebView.getContentHeight() * mWebView.getScale()) - (mWebView.getHeight() + mWebView.getScrollY());
    }

    public interface McEventListener {
        void onEventUp(int scrollY);
        void onEventDown(int scrollY);
        void onEventPageTop(int scrollY);
        void onEventPageEnd(int scrollY);
    }

    private McEventListener mEventListener;

    public void setMcEventListener(McEventListener listener) {
        mEventListener = listener;
    }

    private int lastScrollY;
    private boolean scrollFx = true;//true 向下滑 false 向上滑

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {

            case MotionEvent.ACTION_DOWN:
                lastScrollY = this.getScrollY();

            case MotionEvent.ACTION_MOVE:
                lastScrollY = this.getScrollY();
                if (Math.abs(lastScrollY - this.getScrollY()) > 10) {
                    if (lastScrollY > this.getScrollY()) {//向下滑
                        scrollFx = true;
                        Log.e("测试", "向下滑");
                    } else {//向上滑
                        scrollFx = false;
                        Log.e("测试", "向上滑");
                    }
                    lastScrollY = this.getScrollY();
                    if (scrollFx) {
                        Log.e("测试", "下滑 ");
                        if (mEventListener != null) {
                            mEventListener.onEventDown(lastScrollY);
                        }
                        if (getScrollY() == 0){
                            Log.e("测试", "下滑 到头了");
                            if (mEventListener != null) {
                                mEventListener.onEventPageTop(lastScrollY);
                            }
                        }
                    } else {
                        Log.e("测试", "上滑 ");
                        if (mEventListener != null) {
                            mEventListener.onEventUp(lastScrollY);
                        }
//                    // webview的高度
//                    float webcontent = getContentHeight() * getScale();
//                    // 当前webview的高度
//                    float webnow = getHeight() + getScrollY();
                        if((this.getHeight() + this.getScrollY())>=(this.getContentHeight() * this.getScale())){
                            Log.e("测试", "上滑 到底了");
                            if (mEventListener != null) {
                                mEventListener.onEventPageEnd(lastScrollY);
                            }
                        }
                    }
                }
            case MotionEvent.ACTION_UP:
                lastScrollY = this.getScrollY();
                if (getScrollY() == 0){
                    Log.e("测试", "下滑 到头了");
                    if (mEventListener != null) {
                        mEventListener.onEventPageTop(lastScrollY);
                    }
                }
                if((this.getHeight() + this.getScrollY())>=(this.getContentHeight() * this.getScale())){
                    Log.e("测试", "上滑 到底了");
                    if (mEventListener != null) {
                        mEventListener.onEventPageEnd(lastScrollY);
                    }
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    private OnScrollChangeListener mOnScrollChangeListener;

    public void setOnScrollChangeListener(OnScrollChangeListener listener) {
        this.mOnScrollChangeListener = listener;
    }

    public interface OnScrollChangeListener {
        public void onPageEnd(int l, int t, int oldl, int oldt);
        public void onPageTop(int l, int t, int oldl, int oldt);
        public void onScrollChanged(int l, int t, int oldl, int oldt);
    }
}